//
//  HomeViewController.m
//  Thinkape-iOS
//
//  Created by tixa on 15/4/24.
//  Copyright (c) 2015年 TIXA. All rights reserved.
//home View 

#import "HomeViewController.h"
#import "AppDelegate.h"
#import "LogineViewController.h"
#import "LinkViewController.h"
#import "KindsPickerView.h"
#import "JSBadgeView.h"
#import "AppDelegate.h"
#import "BillsListViewController.h"
#import "SubmitApproveViewController.h"
#import "MyInforViewController.h"
#import "BillsListNewViewController.h"
//#import "StatementViewController.h"

#import "SZCalendarPicker.h"

@class StatementViewController;

@interface HomeViewController ()
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
@property (weak, nonatomic) IBOutlet UILabel *status; 
@property (weak, nonatomic) IBOutlet UIButton *myUnDo;
@property (weak, nonatomic) IBOutlet UIButton *myWaiteApprove;
@property (weak, nonatomic) IBOutlet UIButton *myMsg;

@property (weak, nonatomic) IBOutlet UIView *shouYeView;

@end

@implementation HomeViewController

- (id)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super initWithCoder:aDecoder];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showLoginVC) name:@"logout" object:nil];
        NSLog(@"DA");
    }
    return self;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    AccountModel *model = [DataManager shareManager].account;
    [self initInformation:model];
    

    //通知进入：
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoDetailVC:) name:@"Notice3DTouch" object:nil];
    
    if ([DataManager shareManager].account != nil){
        [self requestHomeData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gestureEnabel = NO;

    if ([DataManager shareManager].account == nil) {
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        NSLog(@"class:%@",[delegate.window.rootViewController class]);
        [self showLoginVC];
    }
    else{
        AccountModel *model = [DataManager shareManager].account;
        [self initInformation:model];
        //[self requestHomeData];
    }

    self.shouYeView.frame =CGRectMake(13, 169,self.view.frame.size.width, 0);
    self.shouYeView.backgroundColor =[UIColor clearColor];

#pragma mark -- 放日历的地方
    SZCalendarPicker *picker = [SZCalendarPicker showOnView:self.shouYeView];
    picker.today=[NSDate date];
    picker.date=picker.today;
    picker.backgroundColor = [UIColor clearColor];
    picker.frame=CGRectMake(0, 0, self.shouYeView.frame.size.width, self.shouYeView.frame.size.height);

    picker.contentMode=UIViewTintAdjustmentModeAutomatic;
    picker.calendarBlock =^(NSInteger day, NSInteger month, NSInteger year){
        
        
        //点击日期调用方法处：
        
        NSLog(@"%ld-%ld-%ld哈哈", year,month,day);
    };
//    轻扫手势
    UISwipeGestureRecognizer *swipeGest =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipestre)];
    swipeGest.direction = UISwipeGestureRecognizerDirectionUp;
    [self.shouYeView addGestureRecognizer:swipeGest];

    //通知进入：
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoDetailVC:) name:@"Notice3DTouch" object:nil];

    
    UITapGestureRecognizer *gar =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
    [self.reportView addGestureRecognizer:gar];
    

    //创建一个UIButton,用来点击用户信息的跳转：
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    myButton.frame = CGRectMake(13, 70, SCREEN_WIDTH - 100, 50);
    myButton.backgroundColor = [UIColor clearColor];
    [self.view addSubview:myButton];
    [myButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

//点击用户信息，跳转用户信息展示界面
- (void)myButtonClick:(UIButton *)sender
{
    MyInforViewController *myinfoVC = [[MyInforViewController alloc]init];
    [self.navigationController pushViewController:myinfoVC animated:YES];
}


-(void)swipestre
{
    NSLog(@"清扫");
}

-(void)tapView
{
    StatementViewController *stateVC =[self.storyboard instantiateViewControllerWithIdentifier:@"StateVC"];
    [self.navigationController pushViewController:stateVC animated:YES];
}

#pragma mark   从快捷菜单进入详细页面
-(void)gotoDetailVC:(NSNotification *)notification
{
    NSString *type = notification.userInfo[@"type"];
    if ([type isEqualToString:@"1"]) {
        
        SubmitApproveViewController *subVC = [self.storyboard instantiateViewControllerWithIdentifier:@"EditCGVC"];
        [self.navigationController pushViewController:subVC animated:YES];
        
    }else if ([type isEqualToString:@"2"]){

        BillsListNewViewController *billVC = [self.storyboard instantiateViewControllerWithIdentifier:@"UnfinishedBills"];
        [self.navigationController pushViewController:billVC animated:YES];
        
    }
}

- (void)showLoginVC{
    LogineViewController *log = [[LogineViewController alloc] init];
    log.logSuccess = ^(AccountModel *model){
        [self initInformation:model];
        [self requestHomeData];
    };
    [self presentViewController:log animated:NO completion:nil];
}

- (void)initInformation:(AccountModel *)model
{
    self.userImageView.image = nil;
//    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:[model.PicPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:Default_Head_Image];
    
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:[model.PicPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]  placeholderImage:Default_Head_Image options:SDWebImageRefreshCached];
    self.userName.text = [NSString stringWithFormat:@"%@，欢迎您",model.Uname];
    NSLog(@"2223333333333333333333333333%@",model.Uname);
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy年MM月dd日";
    self.time.text = [formatter stringFromDate:date];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestHomeData{
    //http://27.115.23.126:3032/Ashx/Mobilenew.ashx?ac=GetFirstInfo&u=1
    
    [RequestCenter GetRequest:[NSString stringWithFormat:@"ac=GetFirstInfo&u=%@",self.uid]
                   parameters:nil
                      success:^(NSURLSessionDataTask *operation, NSDictionary *responseObject) {
                          NSLog(@"respons %@",responseObject);
                          /*
                           {
                           ac: "MyUnReceive", 我的未付款
                
                           },
                           {
                           ac: "Msg",  我的消息
                           num: "100"
                           },
                           {
                           ac: "myWaiteApprove",  我的待审批
                           num: "111"
                           },
                           {
                           ac: "MyUnComplete", 我的未完成
                           num: "111"
                           }
                           
                           */
                          
                          [SVProgressHUD dismiss];
                          NSArray *arr = [[responseObject objectForKey:@"msg"] objectForKey:@"data"];
                          NSString *str = @"";
                          UIButton *tempBtn;
                          NSString *text;
                          for (NSDictionary *dic in arr) {
                              if (/*[[dic objectForKey:@"ac"] isEqualToString:@"审批中"] || */ [[dic objectForKey:@"ac"] isEqualToString:@"MyUnReceive"]) {
                                  
                                  str = [NSString stringWithFormat:@"%@待付款条数:%@   ",str,[dic objectForKey:@"num"]];
                                 // break;
                              }
                              else if ([[dic objectForKey:@"ac"] isEqualToString:@"Msg"]) {
                                  tempBtn = self.myMsg;
                                  if ([[dic objectForKey:@"num"]isEqualToString:@"0"]) {
                                      text=@"";
                                  }
                                  else{
                                  text = [dic objectForKey:@"num"];
                                  }
                              }
                              else if ([[dic objectForKey:@"ac"] isEqualToString:@"MyUnComplete"]){
                                  tempBtn = self.myUnDo;
                                  if ([[dic objectForKey:@"num"]isEqualToString:@"0"]) {
                                    text=@"";
                                      
                                  }else
                                  {
                                  text = [dic objectForKey:@"num"];
                                  }
                              }
                              else if ([[dic objectForKey:@"ac"] isEqualToString:@"myWaiteApprove"]){
                                  tempBtn = self.myWaiteApprove;
                                  if ([[dic objectForKey:@"num"]isEqualToString:@"0"]) {
                                      text=@"";
                                  }
                                  else{
                                  text = [dic objectForKey:@"num"];
                                  }
                              }
                              if (tempBtn) {
                                  JSBadgeView *view = (JSBadgeView *)[tempBtn viewWithTag:1024];
                                  if (!view) {
                                      view = [[JSBadgeView alloc] initWithParentView:tempBtn alignment:JSBadgeViewAlignmentTopRight];
                                      view.tag = 1024;
                                  }
                                  view.badgeText = text;
                              }
                          }
                          if (str.length == 0) {
                              str = @"审批中:元   待付款:元";
                          }
                         self.status.text = str;
                      }
                      failure:^(NSURLSessionDataTask *operation, NSError *error) {
                          
                      }
            showLoadingStatus:YES];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if([segue.identifier isEqualToString:@"unComplantToList"]){
        id toVC = segue.destinationViewController;
        [toVC setValue:@"未完成审批" forKey:@"titleStr"];
    
        [toVC setValue:[NSNumber numberWithBool:NO] forKey:@"undo"];
        self.istobe=NO;
        //wo
        AppDelegate * app= (AppDelegate *)[UIApplication sharedApplication].delegate;
        app.danJu=@"未完成审批";
    }
    
    if ([segue.identifier isEqualToString:@"complantToList"]) {
        id toVC = segue.destinationViewController;
        
//        [toVC setValue:@"已完成审批" forKey:@"titleStr"];
        [toVC setValue:@"我的已完成" forKey:@"titleStr"];
        [toVC setValue:[NSNumber numberWithBool:YES] forKey:@"undo"];
        self.istobe=YES;
//        //wo
        AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
//        app.danJu=@"已完成审批";
         app.danJu=@"已完成审批";
    }
    
    if ([segue.identifier isEqualToString:@"LinkVC"]) {
        LinkViewController *link = (LinkViewController *)segue.destinationViewController;
        link.linkStyle = 0;
        link.titleStr = @"联系人";
    }
    
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
